// This is autogenerated file. Do not edit!

package models.autogenerated;

class SentencesManager
{
	var db : orm.Db;
	var orm : models.Orm;
	public var query(get, never) : orm.SqlQuery<models.Sentences>;

	function get_query() : orm.SqlQuery<models.Sentences>
	{
		return new orm.SqlQuery<models.Sentences>("sentences", db, this);
	}

	public function new(db:orm.Db, orm:models.Orm) : Void
	{
		this.db = db;
		this.orm = orm;
	}

	function newModelFromParams(id:String, sentence:String, author_id:String, publish_date:Float, edit_date:Float, day_popularity:Int, total_popularity:Int, last_access:Float) : models.Sentences
	{
		var _obj = new models.Sentences(db, orm);
		_obj.id = id;
		_obj.sentence = sentence;
		_obj.author_id = author_id;
		_obj.publish_date = publish_date;
		_obj.edit_date = edit_date;
		_obj.day_popularity = day_popularity;
		_obj.total_popularity = total_popularity;
		_obj.last_access = last_access;
		return _obj;
	}

	function newModelFromRow(d:Dynamic) : models.Sentences
	{
		var _obj = new models.Sentences(db, orm);
		_obj.id = Reflect.field(d, 'id');
		_obj.sentence = Reflect.field(d, 'sentence');
		_obj.author_id = Reflect.field(d, 'author_id');
		_obj.publish_date = Reflect.field(d, 'publish_date');
		_obj.edit_date = Reflect.field(d, 'edit_date');
		_obj.day_popularity = Reflect.field(d, 'day_popularity');
		_obj.total_popularity = Reflect.field(d, 'total_popularity');
		_obj.last_access = Reflect.field(d, 'last_access');
		return _obj;
	}

	public function where(field:String, op:String, value:Dynamic) : orm.SqlQuery<models.Sentences>
	{
		return query.where(field, op, value);
	}

	public function get(id:String) : models.Sentences
	{
		return getBySqlOne('SELECT * FROM `sentences` WHERE `id` = ' + db.quote(id));
	}

	public function create(id:String, sentence:String, author_id:String, publish_date:Float, edit_date:Float, day_popularity:Int, total_popularity:Int, last_access:Float) : models.Sentences
	{
		db.query('INSERT INTO `sentences`(`id`, `sentence`, `author_id`, `publish_date`, `edit_date`, `day_popularity`, `total_popularity`, `last_access`) VALUES (' + db.quote(id) + ', ' + db.quote(sentence) + ', ' + db.quote(author_id) + ', ' + db.quote(publish_date) + ', ' + db.quote(edit_date) + ', ' + db.quote(day_popularity) + ', ' + db.quote(total_popularity) + ', ' + db.quote(last_access) + ')');
		return newModelFromParams(id, sentence, author_id, publish_date, edit_date, day_popularity, total_popularity, last_access);
	}

	public function createNamed(data:{ id:String, sentence:String, author_id:String, publish_date:Float, edit_date:Float, day_popularity:Int, total_popularity:Int, last_access:Float }) : models.Sentences
	{
		db.query('INSERT INTO `sentences`(`id`, `sentence`, `author_id`, `publish_date`, `edit_date`, `day_popularity`, `total_popularity`, `last_access`) VALUES (' + db.quote(data.id) + ', ' + db.quote(data.sentence) + ', ' + db.quote(data.author_id) + ', ' + db.quote(data.publish_date) + ', ' + db.quote(data.edit_date) + ', ' + db.quote(data.day_popularity) + ', ' + db.quote(data.total_popularity) + ', ' + db.quote(data.last_access) + ')');
		return newModelFromParams(data.id, data.sentence, data.author_id, data.publish_date, data.edit_date, data.day_popularity, data.total_popularity, data.last_access);
	}

	public function createOptional(data:{ id:String, ?sentence:String, ?author_id:String, ?publish_date:Float, ?edit_date:Float, ?day_popularity:Int, ?total_popularity:Int, ?last_access:Float }) : models.Sentences
	{
		createOptionalNoReturn(data);
		return get(data.id);
	}

	public function createOptionalNoReturn(data:{ id:String, ?sentence:String, ?author_id:String, ?publish_date:Float, ?edit_date:Float, ?day_popularity:Int, ?total_popularity:Int, ?last_access:Float }) : Void
	{
		var fields = [];
		var values = [];
		fields.push('`id`'); values.push(db.quote(data.id));
		if (Reflect.hasField(data, 'sentence')) { fields.push('`sentence`'); values.push(db.quote(data.sentence)); }
		if (Reflect.hasField(data, 'author_id')) { fields.push('`author_id`'); values.push(db.quote(data.author_id)); }
		if (Reflect.hasField(data, 'publish_date')) { fields.push('`publish_date`'); values.push(db.quote(data.publish_date)); }
		if (Reflect.hasField(data, 'edit_date')) { fields.push('`edit_date`'); values.push(db.quote(data.edit_date)); }
		if (Reflect.hasField(data, 'day_popularity')) { fields.push('`day_popularity`'); values.push(db.quote(data.day_popularity)); }
		if (Reflect.hasField(data, 'total_popularity')) { fields.push('`total_popularity`'); values.push(db.quote(data.total_popularity)); }
		if (Reflect.hasField(data, 'last_access')) { fields.push('`last_access`'); values.push(db.quote(data.last_access)); }
		db.query('INSERT INTO `sentences`(' + fields.join(", ") + ') VALUES (' + values.join(", ") + ')');
	}

	public function delete(id:String) : Void
	{
		db.query('DELETE FROM `sentences` WHERE `id` = ' + db.quote(id) + ' LIMIT 1');
	}

	public function getAll(_order:String=null) : Array<models.Sentences>
	{
		return getBySqlMany('SELECT * FROM `sentences`' + (_order != null ? ' ORDER BY ' + _order : ''));
	}

	public function getBySqlOne(sql:String) : models.Sentences
	{
		var rows = db.query(sql + ' LIMIT 1');
		if (rows.length == 0) return null;
		return newModelFromRow(rows.next());
	}

	public function getBySqlMany(sql:String) : Array<models.Sentences>
	{
		var rows = db.query(sql);
		var list : Array<models.Sentences> = [];
		for (row in rows)
		{
			list.push(newModelFromRow(row));
		}
		return list;
	}
}